var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
static var rotateSpeed : float = 3.0;

function Awake()
{
	//Keeps the body from falling over
	rigidbody.freezeRotation = true;
}
function Update()
{
	//Rotates the body with the mouse
	transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed, 0);
}

function FixedUpdate()
{
	//If the body is in contact with the ground
	 if (grounded)
    {

		//Instantiates a Vector3 class with input from the keyboard and processes it.
        var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        targetVelocity = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;
        
		//Reads the velocity of the object and subtracts the maximum velocity from the current velocity.
		//This allows the object to quickly stop moving when the input key is released.
        var velocity = rigidbody.velocity;
        var velocityChange = (targetVelocity - velocity);
        //velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        //velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        //velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
		
		//Jump 
		if(Input.GetButton("Jump"))
		{
			rigidbody.AddForce(Vector3.up * jumpHeight * 100);
		}
    }

    grounded = false;
	
}
function OnCollisionStay()
{
	grounded = true;
	
}

function OncollisionExit()
{
	grounded = false;
}